/**
 * 
 */
package com.tech.freak.pyp;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * @author julian
 *
 */
public class DateChangedReceiver extends BroadcastReceiver {

	private static final String TAG = "DateChangedReceiver";

	/* (non-Javadoc)
	 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
	 */
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "intent=" + intent);

        // For our example, we'll also update all of the widgets when the timezone
        // changes, or the user or network sets the time.
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)
                || action.equals(Intent.ACTION_TIME_CHANGED)
                || action.equals(Utils.UPDATE_WIDGET)
                || action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            AppWidgetManager gm = AppWidgetManager.getInstance(context);

            int [] appWidgetIds = gm.getAppWidgetIds(new ComponentName("com.tech.freak.pyp","com.tech.freak.pyp.PyPAppWidgetProvider"));
            
            final int N = appWidgetIds.length;
            for (int i=0; i<N; i++) {
            	int appWidgetId = appWidgetIds[i];
            	Log.d(TAG, "Updating widget"+appWidgetId);
            	String city = Utils.getWidgetInfo(context, appWidgetId, Utils.CITY);
            	String license = Utils.getWidgetInfo(context, appWidgetId, Utils.LICENSE);
                PyPAppWidgetProvider.updatePyPWidget(context, gm, appWidgetId, city, license);
            }
        }
    }
}
